//
//  EnterTableViewCell.m
//  ToWhere
//
//  Created by dllo on 15/11/18.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "EnterTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>

@interface EnterTableViewCell()

@property(nonatomic, retain) UIImageView *imageview;

@property(nonatomic, retain) UIImageView *image;

@property(nonatomic, retain) UILabel *titleLabel;

@property(nonatomic, retain) UILabel *detailLabel;

@end

@implementation EnterTableViewCell

- (void)dealloc{
    [_enterModel release];
    [_imageview release];
    [_image release];
    [_titleLabel release];
    [_detailLabel release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.imageview = [[UIImageView alloc]init];
        self.imageview.layer.masksToBounds = YES;
        self.imageview.layer.cornerRadius = 3 * kWidth;
        [self.contentView addSubview:self.imageview];
        [_imageview release];
        
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.font = [UIFont systemFontOfSize: 21 * kWidth];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        self.titleLabel.shadowColor = [UIColor colorWithWhite:0.185 alpha:1.000];
        [self.contentView addSubview:self.titleLabel];
        [_titleLabel release];
        
        self.detailLabel = [[UILabel alloc]init];
        self.detailLabel.font = [UIFont systemFontOfSize:17 * kWidth];
        self.detailLabel.textColor = [UIColor colorWithWhite:0.953 alpha:1.000];
        [self.contentView addSubview:self.detailLabel];
        [_detailLabel release];
        
        self.image = [[UIImageView alloc]init];
        self.image.layer.masksToBounds = YES;
        self.image.layer.cornerRadius = kWidth * 25;
        self.image.layer.borderWidth = 2;
        self.image.layer.borderColor = [UIColor whiteColor].CGColor;
        [self.contentView addSubview:self.image];
        [_image release];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.imageview.frame = CGRectMake(5 * kWidth, 8 * kHeight, self.frame.size.width - 10 * kWidth, self.frame.size.height - 8 * kHeight);
    CGFloat height = [EnterTableViewCell heightOfLabel:self.titleLabel.text andTextFont:[UIFont fontWithName:@"Helvetica-Bold" size:21 * kWidth] andTextWidth:self.frame.size.width - 50 * kWidth];
    self.titleLabel.frame = CGRectMake(15 * kWidth, 15 * kHeight, self.frame.size.width - 50 * kWidth, height);
    self.detailLabel.frame = CGRectMake(15 * kWidth, self.titleLabel.frame.size.height + 17 * kHeight, self.titleLabel.frame.size.width, 30 * kHeight);
    self.image.frame = CGRectMake(15 * kWidth, self.frame.size.height - 60 * kHeight, 50 * kWidth, 50 * kWidth);
}

-(void)setEnterModel:(EntertainmentModel *)enterModel
{
    if (_enterModel != enterModel) {
        [_enterModel release];
        _enterModel = [enterModel retain];
    }
    [self.imageview sd_setImageWithURL:[NSURL URLWithString:enterModel.front_cover_photo_url] placeholderImage:[UIImage imageNamed:@"placeHoder"]];
    self.titleLabel.text = enterModel.name;
    self.detailLabel.text = [NSString stringWithFormat:@"%@/%@天/%@图", enterModel.start_date, enterModel.days, enterModel.photos_count];
    [self.image sd_setImageWithURL:[NSURL URLWithString:[enterModel.user objectForKey:@"image"]] placeholderImage:[UIImage imageNamed:@"placeHoder"]];
}

//自适应高度
+(CGFloat)heightOfLabel:(NSString *)text andTextFont:(UIFont *)font andTextWidth:(CGFloat)width
{
    CGSize size = CGSizeMake(width, MAXFLOAT);
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dictionary context:nil];
    return frame.size.height;
}

@end
